home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Arsenal Files 6
/
The Arsenal Files 6 (Arsenal Computer).ISO
/
prg_casm
/
tran20.zip
/
TRAN.DOC
< prev
next >
Wrap
Text File
|
1996-01-09
|
20KB
|
763 lines
TRAN PROGRAMMER'S MANUAL
VERSION 2.0
(TechniLib Random Number Generators)
TechniLib Company
Copyright 1995, 1996 by TechniLib (TM) Company
All Rights Reserved
TERMS OF USE AND DISTRIBUTION
TRAN is a shareware product; therefore, unregistered copies of TRAN
are made available free of charge so that potential purchasers will have
the opportunity to examine and test the software before committing payment.
Distribution of unregistered copies of TRAN to other potential users is
also permitted and appreciated. However, usage and distribution of TRAN
must conform to the following conditions. In the following statement, the
term "commercial distribution," includes shareware distribution.
1) TRAN and accompanying software must be distributed together in copies of
the original archive provided by TechniLib. Neither the archive nor
individual files therein may be modified.
2) The TRAN archive may be distributed in combination with other shareware
products; however, the TRAN archive may not be distributed with other
commercially distributed software without written consent of TechniLib.
3) Copies of TRAN which have been used to develop software for commercial
distribution must be registered before such software is marketed. Copies
of TRAN which have been used to develop noncommercial software must be
registered if such software is to be regularly used either by the developer
or others.
4) Commercially distributed software must embed TRAN procedures in the
software code. Files contained in the TRAN archive may not be placed in
the distribution media.
5) TRAN is designed to offer a set of services to other executable code.
TRAN may not be used to develop software for commercial distribution which
will essentially offer any of these same services to other executable code.
Exceptions to this condition require written consent of TechniLib.
6) Rights afforded by registering a single copy of TRAN pertain only to a
single computer.
7) TRAN may be registered for a fee of $25.00 per copy. Consult README.DOC
in the TRAN archive for further instructions regarding registration.
DISCLAIMER OF WARRANTY
TRAN AND ALL ACCOMPANYING SOFTWARE AND LITERATURE ARE DISTRIBUTED WITH
THE EXCLUSION OF ANY AND ALL IMPLIED WARRANTIES, AND WITH THE EXCLUSION OF
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
TechniLib SHALL HAVE NO LIABILITY FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES RESULTING FROM THE USE OF TRAN OR ACCOMPANYING MATERIALS. The user
assumes the entire risk of using this software.
Copyright 1995, 1996 by TechniLib (TM) Company
All Rights Reserved
Introduction
------------
TRAN is a library of procedures designed to generate random numbers
for various probability densities. TRAN specifically generates random
numbers for the uniform, normal (Gaussian), gamma, beta, Cauchy
(Lorentzian), binomial, geometric, Pascal (negative binomial), and poisson
distributions. Random variables for other popular distributions can
generally be obtained from transformations of these. This document
explains how to use such methods to generate variates for the chi-squared,
F, T, exponential, Gumbel, Pareto, Weibull, and logistic distributions.
TRAN is exceptionally fast because it is uses efficient algorithms
that are entirely written in assembly language. The code is also designed
to fully utilize the parallel processing capabilities of the Pentium
processor. Speed is potentially an important consideration since random
number generators can be called with great frequency in simulation
programs. A considerable amount of arithmetic is often needed to generate
random numbers; consequently, inefficient code can be costly in terms of
CPU time.
TRAN code is extremely compact. Notwithstanding its many functions,
TRAN occupies a little over 3K of RAM. Such compactness is another benefit
of assembly language.
TRAN algorithms produce random variables of high quality. It is well
known that many random number generators produce numbers failing to meet
important tests of randomness. TRAN's uniform variate generator uses dual
linear congruential generators and a shuffle table to produce high-quality
variates. Since all of the other generators are based upon the uniform
generator, they too possess a high degree of randomness. All continuous
generators in TRAN have a period of 2.3058e18.
The TRAN archive contains libraries for Microsoft, Borland, and Watcom
languages. These libraries are compatible with both DOS and Windows.
Libraries for Microsoft are in the file MICROSOF.ZIP. Libraries for
Borland are in BORLAND.ZIP. Watcom libraries are in WATCOM.ZIP. These
archives contain files called TRAN.LIB and REGISTRD.ZIP. TRAN.LIB contains
random number procedures for the small memory model. REGISTRD.ZIP contains
libraries for small, medium, compact, and large memory models.
REGISTRD.ZIP is password protected. The password is supplied upon
registration of TRAN. Refer to README.DOC for registration information.
TRAN.LIB should be used to test and examine the software before
registration.
REGISTRD.ZIP contains files named TRANx.LIB where x equals either s,
m, c, or l; corresponding to the small, medium, compact, and large memory
models. The remaining files in REGISTRD.ZIP are called FPUTRANx.LIB for x
= s, m, c, and l. These libraries are like the TRANx libraries except they
assume the presence of a math coprocessor. They are smaller and faster
than the TRANx libraries.
1
The following C prototypes summarize the major procedures in TRAN:
Procedure Purpose
--------- -------
void setseed(long seed) Initialize all generators
double rndu(void) Generate uniform variate on (0,1)
double rnd_normal(void) Generate standard normal variate
void parm_gamma(double r) Parameterize gamma generator
double rnd_gamma(void) Generate gamma variate
void parm_beta(double a, double b) Parameterize beta generator
double rnd_beta(void) Generate beta variate
double rnd_cauchy(void) Generate standard Cauchy variate
void parm_binomial(ulong n, double p) Parameterize binomial generator
ulong rnd_binomial(void) Generate binomial variate
void parm_pascal(ulong n, double p) Parameterize Pascal generator
ulong rnd_pascal(void) Generate Pascal variate
void parm_poisson(double theta) Parameterize poisson generator
ulong rnd_poisson(void) Generate poisson variate
Observe that the generator functions never accept arguments. If
parameters are needful, then the generator is parameterized by a call to a
separate function. This approach enables faster execution.
The parameterization functions do not check arguments for validity;
consequently, error codes are not returned. Invalid parameters will
generally lead to floating point unit (FPU) exceptions.
See EXAMPLE.C in the TRAN archive for an example program using TRAN.
Initializing TRAN
-----------------
All TRAN generators should be initialized by a call to setseed(). The
prototype for this function is:
void setseed(ulong seed);
where seed is any nonzero number. If setseed() is never called, or called
with seed = 0, then a seed value of 1 is used instead. A unique sequence
of random numbers is associated with each seed value.
Because of the nature of probability computations, extremely small
numbers are sometimes obtained as intermediate results. These small
numbers are typically harmless, but can generate FPU exceptions unless the
FPU is appropriately configured. The control word in the FPU should be set
to mask exceptions for underflow and denormalized operands. TRAN contains
two procedures to handle this task. These are setfpucw() and resetfpucw().
Neither procedure has parameters nor return values. setfpucw()
appropriately sets the FPU control word for TRAN. resetfpucw() restores
the original control word.
2
TRAN Generators for Continuous Variates
---------------------------------------
This section discusses variates from continuous distributions which
can be directly simulated with TRAN. Later sections discuss
transformations which enable simulation of other distributions.
Uniform Variates
----------------
Uniform variates are returned by rndu(). The return values are
always between zero and one. Zero and one are never themselves returned.
Uniform random numbers over (a, b) can be generated with:
x = (b - a) rndu() + a
Normal (Gaussian) Variates
--------------------------
Standard normal variates are returned by rnd_normal(). A normal
variate with mean µ and standard deviation σ can be generated with:
x = σ rnd_normal() + µ
Gamma Variates
--------------
The gamma density has the form:
r r-1 -Θx
f(x) = [Θ / Γ(a)] x e x, r, Θ > 0
where Γ is the complete gamma function. TRAN only generates values
corresponding to Θ = 1 because variates for other Θ can be obtained by
dividing TRAN return values by Θ.
The gamma generator must first be parameterized by a call to
parm_gamma(). This function informs the generator as to the appropriate
value of r. The C prototype is:
void parm_gamma(double r);
rnd_gamma() may then be called to obtain the gamma variates.
Unlike many generators, rnd_gamma() can generate values for r < 1.
For small integer values of r, it may be faster to generate gamma
variates with:
x = -log[U1 U2 ... Ur] / Θ
where log() is the natural log, and U1...Ur are uniform variates in (0,1).
3
Beta Variates
-------------
The beta density has the form:
a-1 b-1
f(x) = [1 / ß(a,b)] x (1 - x) a, b > 0; 0 < x < 1
where ß() is the beta function. To generate beta variates, one must first
parameterize the generator with a call to parm_beta(). The function has C
prototype:
void parm_beta(double a, double b);
Beta variates can then be obtained by calling rnd_beta().
Unlike many beta generators, rnd_beta() can generate variates when
either a < 1 or b < 1 or both.
Cauchy (Lorentzian) Variates
----------------------------
The Cauchy density is specified by:
1
f(x) = ---------------------- σ > 0
σπ[1 + [(x - µ) / σ]²]
Cauchy variates for µ = 0 and σ = 1 can be generated by calling
rnd_cauchy(). For other values of µ and σ, use:
x = σ rnd_cauchy() + µ
4
TRAN Generators for Discrete Variates
-------------------------------------
The primary discrete variate generators in TRAN are based upon the
rejection method using a Cauchy comparison function. This method is highly
efficient for most cases; however, even faster methods can be found for
specific parameter values. For example, binomial variates under small
values of n can be generated most efficiently by summing n Bernoulli
variates. This approach could be considerably faster than the rejection
method under small n, but many times slower under large n.
TRAN includes secondary generators for all discrete distributions to
accommodate special parameterizations where the rejection method may be
relatively slow. The relative speeds of the primary and secondary
generators can vary from one parameterization to another and from one CPU
to another. Unfortunately, there are no "simple" rules offering perfect
guidance when choosing between generators. It is therefore recommended
that choice be based upon experimentation.
Binomial Variates
-----------------
The binomial distribution is defined by:
n! x n-x
f(x) = ----------- p (1 - p) 0 < p < 1; n > 0; x = 0,1,,,n
x! (n - x)!
where ! denotes factorial. To generate binomial variates, one should
first parameterize the generator with a call to parm_binomial(). This
function has C prototype:
void parm_binomial(ulong n, double p);
Binomial variates are then generated by calling rnd_binomial(). The
generator function returns an unsigned long integer.
The secondary binomial generator computes variates by summing n
Bernoulli variates. This generator will be fast when n is small. It
should not be used when n is large. The parameterization function and
generator have C prototypes:
void parm_binomial2(uint n, double p);
uint rnd_binomial2(void);
When n is large and np is small, the binomial is well approximated by
the poisson distribution. See the section on poisson variates for details.
5
Geometric Variates
------------------
The geometric distribution is defined by:
x
f(x) = (1 - p) p 0 < p < 1; x = 0,1,,,
The geometric distribution is the special case of the Pascal (negative
binomial) distribution when r = 1 (see next section). One may therefore
generate geometric variates using the Pascal generator. However, the
fastest way to generate geometric variates is with:
x = (int)[log(U) / log(1 - p)]
where U is a uniform variate in (0,1), and (int) denotes a type cast which
truncates the fractional part of its argument.
Pascal (Negative Binomial) Variates
-----------------------------------
The Pascal density has the form:
(x + r - 1)! r x
f(x) = ------------ p (1 - p) 0 < p < 1; r > 0; x = 0,1,,,
x! (r - 1)!
The Pascal generator must be parameterized with a call to
parm_pascal(). This function has C prototype:
void parm_pascal(ulong r, double p);
The variates are then generated by calling rnd_pascal(). The generator
function returns an unsigned long integer.
The secondary pascal generator computes variates by summing r
geometric variates. This generator will be fast when r is small. It
should not be used when r is large. The parameterization function and
generator have C prototypes:
void parm_pascal2(uint r, double p);
uint rnd_pascal2(void);
6
Poisson Variates
----------------
The poisson density is specified by:
-Θ x
f(x) = e Θ / x! Θ > 0; x = 0,1,,,
The poisson generator is parameterized with a call to parm_poisson().
This function has C prototype:
void parm_poisson(double theta);
Poisson variates are obtained by calling rnd_poisson(). This function
returns an unsigned long integer.
The secondary poisson generator obtains a poisson variate by counting
the number of exponential variates necessary to produce a sum greater than
Θ. This approach is highly effective when Θ is small. The C prototypes
for the parameterization function and generator are:
void parm_poisson2(double theta);
uint rnd_poisson2(void);
The binomial distribution with parameters n and p converges to a
poisson with parameter np as p / n converges to zero. Therefore with large
n or small p, the cumbersome binomial can be replaced with the poisson. As
a general rule, the approximation is "good" when n > 100 and p < .10.
7
Generating Variates in the Normal Family
----------------------------------------
Chi-Squared Variates
--------------------
Let G be a gamma variate with parameter r = d / 2, then 2G is a chi-
squared variate with d degrees of freedom.
A chi-squared variate with d degrees of freedom can also be generated
with:
2 2 2
x = N1 + N2 +...+ Nd
where N1...Nd are standard normal variates.
F Variates
----------
Let B be a beta variate with parameters a = d / 2 and b = n / 2, then:
x = d (1 - B) / nB
is an F variate with n degrees of freedom in the numerator and d degrees of
freedom in the denominator.
T Variates
----------
Let F be an F variate with 1 degree of freedom in the numerator and d
degrees of freedom in the denominator. Let I = 1 or -1 with 50%
probability for each (I is independent of F), then a random T variate with
d degrees of freedom is generated by:
1/2
x = I F
8
Generating Variates Via the Inverse Method
------------------------------------------
Let f() be a probability density with cumulative density F(). Let
F() be invertible, and let U be a uniform variate in (0,1), then:
-1
x = F(U)
is distributed according to density f. In the remainder of this section,
we present distributions which can be simulated using this method.
Exponential Variates
--------------------
-Θx
f(x) = Θ e x, Θ > 0
generator: x = -log(U) / Θ
Exponential variates can also be generated from the gamma
distribution. The exponential is a special case of the gamma with r = 1.
Pareto Variates
---------------
Θ -(Θ+1)
f(x) = Θ a x Θ, a > 0; x > a
1/Θ
generator: x = a / U
Gumbel (Extreme Value) Variates
-------------------------------
-(x - α)/ß
-e
F(x) = e ß > 0
generator: x = α - ß log[-log(U)]
9
Logistic Variates
-----------------
1
F(x) = --------------------- ß > 0
-(x-α)/ß
1 + e
generator: x = α + ß log(U)
Weibull Variates
----------------
b
b-1 -ax
f(x) = a b x e a, b, x > 0
1/b
generator: x = [-log(U) / a]
10